package tec.code;

import java.util.Iterator;

public class GB2312 implements Encode {
	public Iterator<Integer> iterator() {
		return(new CharIterator());
	}

	@Override
	public String getName() {
		return("GB2312");
	}
	
	private static class CharIterator implements Iterator<Integer> {
		public CharIterator() {
			current = HEAD;
		}
		
		@Override
		public boolean hasNext() {
			return(current <= TAIL);
		}

		@Override
		public Integer next() {
			while((! isCodePoint(current)) && (current <= TAIL)) current++;
			return(current++);
		}
		
		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}
	
		private boolean isCodePoint(int c) {
			if((c & 0x0000FF00) == 0xD700) {
				return(inRange((c & 0x000000FF), 0xA1, 0xF9));
			} else {
				return(
					(inRange((c & 0x0000FF00), 0xB000, 0xF700) && inRange((c & 0x000000FF), 0xA1, 0xFE))
				);
			}
		}
		
		private boolean inRange(int c, int begin, int end) {
			return(c >= begin && c <= end);
		}
		
		private int current;
		
		private static final int HEAD = 0xB0A1;
		private static final int TAIL = 0XF7FF;
	}
}
